//
//  XDynamicViewController.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/9/9.
//

#import "XDynamicViewController.h"
#import "XDynamicRecordTableViewCell.h"
#import "XDynamicBookkeepingTableViewCell.h"

#import <LYEmptyView/LYEmptyView.h>
#import <LYEmptyView/UIView+Empty.h>

#import "XDynamicModel.h"
#import "XRcordDetailsViewController.h"

#import "RFaceCheckRequest.h"
#import "QXAdvertManager.h"

@interface XDynamicViewController ()<UITableViewDelegate,UITableViewDataSource,QXAdvertDelegate>

@property(nonatomic,strong)MyLinearLayout *rootLayout;

@property(nonatomic,strong)UIView* adView;

/** 列表*/
@property(nonatomic,strong)UITableView* tableView;


@property(nonatomic,strong)RLMResults<XDynamicModel*> *datas;

@end

@implementation XDynamicViewController

- (void)loadView{
    [super loadView];
    MyLinearLayout *rootLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    rootLayout.myMargin = 0;
    [self.view addSubview:rootLayout];
    _rootLayout = rootLayout;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupViews];
    
    self.adView.visibility = MyVisibility_Gone;
    
    [self loadBannerAd];
}

//banner广告
- (void)loadBannerAd{
    //获取banner广告
    [QXAdvertManager manager].delegate = self;
    [[QXAdvertManager manager] loadBannerAdWithRootViewController:self superView:self.adView];
}

-(void)setupViews{
    
    UIView* adView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, PX(64))];
    adView.myMargin = 0;
    adView.myHeight = PX(64);
    [self.rootLayout addSubview:adView];
    _adView = adView;
    
    //列表
    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - IPHONE_NAVIGATION_BAR_HEIGHT - IPHONE_TAB_BAR_HEIGHT-PX(44.0)-PX(64)) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = COLOR_BACKGEOUND;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.estimatedRowHeight = PX(100);
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.sectionFooterHeight = CGFLOAT_MIN;
    tableView.sectionHeaderHeight = 10;
    tableView.tableFooterView = [UIView new];
    [tableView registerClass:NSClassFromString(dynamicRecordCell) forCellReuseIdentifier:dynamicRecordCell];
    [tableView registerClass:NSClassFromString(dynamicBookeepingCell) forCellReuseIdentifier:dynamicBookeepingCell];
    tableView.myMargin = 0;
    tableView.weight = 1.0;
    [self.rootLayout addSubview:tableView];
    _tableView = tableView;
    
    LYEmptyView* emptyView = [LYEmptyView emptyViewWithImageStr:NULL titleStr:@"快乐生活每一天" detailStr:@"快来记录每天不一样的自己"];
    emptyView.titleLabFont = FONT_FIT(12);
    emptyView.titleLabTextColor = COLOR_BLACK_DARK;
    emptyView.detailLabFont = FONT_FIT(12);
    emptyView.detailLabTextColor = COLOR_GRAY_BASE;
    emptyView.detailLabMargin = 10;
    emptyView.contentViewOffset = -100;
    tableView.ly_emptyView = emptyView;
}

#pragma mark -- reloadData
// 刷新数据
- (void)reloadData{
    RLMResults<XDynamicModel*> *models = [[XDynamicModel allObjects] sortedResultsUsingKeyPath:@"date" ascending:NO];
    self.datas = models;
    [self.tableView reloadData];
}

#pragma mark --UITableViewDelegate  UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XDynamicModel* dynamic = [self.datas objectAtIndex:indexPath.section];
    
    if(XDynamicTypeRecord == dynamic.type){
        XDynamicRecordTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:dynamicRecordCell forIndexPath:indexPath];
        cell.dynamic = dynamic;
        return cell;
    }else{
        XDynamicBookkeepingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dynamicBookeepingCell forIndexPath:indexPath];
        cell.dynamic = dynamic;
        return cell;
    }

    return [UITableViewCell new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    XDynamicModel* dynamic = [self.datas objectAtIndex:indexPath.section];
    
    if(XDynamicTypeRecord == dynamic.type){
        XRcordDetailsViewController* detailsController = [XRcordDetailsViewController new];
        detailsController.record = [self.datas objectAtIndex:indexPath.section];
        [UIViewController showViewController:detailsController animated:YES];
    }else{

    }
}

#pragma mark -- <QXadverSDKDelegate>
//获取广告类型
- (void)QXAdTypeDidLoaded:(ADType)adType success:(BOOL)success{
    if(!success && adType == ADType_Banner){
        self.adView.visibility = MyVisibility_Gone;
    }
}

//加载成功
- (void)QXAdDidLoaded:(ADType)adType adChannel:(ADChannel)adChannel ad:(id)ad{
    if(adType == ADType_Banner){
        self.adView.visibility = MyVisibility_Visible;
    }
}

//加载失败
- (void)QXAdLoadFaild:(ADType)adType adChannel:(ADChannel)adChannel error:(NSError *)error{
    if(adType == ADType_Banner){
        self.adView.visibility = MyVisibility_Gone;
    }
}

//展示失败
- (void)QXAdDisplayFaild:(ADType)adType adChannel:(ADChannel)adChannel error:(NSError *)error{
    if(adType == ADType_Banner){
        self.adView.visibility = MyVisibility_Gone;
    }
}

//关闭
- (void)QXAdDidClosed:(ADType)adType adChannel:(ADChannel)adChannel ad:(id)ad{
    if(adType == ADType_Banner){
        self.adView.visibility = MyVisibility_Gone;
    }
}


#pragma mark -- check

-(void)requestData{
    //getEtc 3-a 4-b
    RFaceCheckRequest* request = [RFaceCheckRequest new];
    [RequestOperation request:request quite:YES callBack:^(HResponse * _Nullable response) {
        [response success:^(NSArray*  _Nonnull data) {
            if(data.count == 4){
                [[NSNotificationCenter defaultCenter] postNotificationName:kChangeNotification object:NULL userInfo:NULL];
            }else{
                [self reloadData];
            }
        }];
    }];
}
@end
